home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / save_file.c < prev    next >
C/C++ Source or Header  |  1997-08-21  |  1KB  |  47 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main (int argc, char **argv);
  5.  
  6. int main (int argc, char **argv)
  7.   {
  8.   char name[80];
  9.   int i = 0;
  10.   /**
  11.     arg 1:  filename to save from current directory
  12.     arg 2:  directory to move the file to
  13.  
  14.     1) check arguments
  15.     2) find a valid filename in the directory
  16.     3) copy the file
  17.   **/
  18.   if( argc != 3 )
  19.     {
  20.     printf("Usage:  %s <file> <dir>\n",argv[0]);
  21.     return (100);
  22.     };
  23.   if( access( argv[1], R_OK ) != 0 )
  24.     {
  25.     perror("save_file<file>");
  26.     (void)poserr("save_file <file>");
  27.     return (100);
  28.     };
  29.   if( access( argv[2], R_OK ) != 0 )
  30.     {
  31.     perror("save_file<dir>");
  32.     (void)poserr("save_file<dir>");
  33.     return (100);
  34.     };
  35.   sprintf(name,"%s/%s.%06d", argv[2], argv[1], i++);
  36.   while (access(name, F_OK) != -1)   sprintf(name,"%s/%s.%06d", argv[2], argv[1], i++);
  37.   if( rename( argv[1], name) )
  38.     {
  39.     printf("Failed to rename %s to %s\n",argv[1], name);
  40.     perror("save_file<rename>");
  41.     (void)poserr("save_file<rename>");
  42.     return (100);
  43.     };
  44.   return 0;
  45.   }
  46.  
  47.